package br.unifor.cct.mia.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import br.unifor.cct.mia.evolutionary.FeatureConstructionGenotype;
import br.unifor.cct.mia.ga.util.FeatureConstructionGene;

public class SaveReportFC {
	private BufferedWriter writer;
	private File file;
	
	public SaveReportFC(String name, FeatureConstructionGenotype gen, String struc) throws IOException{	    
		file = new File(name);
		writer = new BufferedWriter(new FileWriter(file));
				
		createRelationStructure(struc,"struc",gen);
	}
		
	public void createRelationStructure(String strucFile, String name, FeatureConstructionGenotype indiv) throws IOException {
		
		addLine("@relation "+name+"\n\n");
		
		FileInputStream fIn;
		try {
			fIn = new FileInputStream(strucFile);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fIn));
			
			String atribute = "@attribute ";			
			atribute += "AttributeFC" + " ";
			atribute += "{";
			for (int x=0;x<indiv.getGene().length+1;x++) {
				atribute += x;
				if ( x+1 != indiv.getGene().length+1 ) {
					atribute += ",";
				}
			}
			atribute += "}\n";						
			addLine(atribute);
			
			String line = reader.readLine();
			while ( line != null ) {
				String[] tokens = line.split(" ");
				
				 atribute = "@attribute ";
				String attName = tokens[0];
				String attType = tokens[1];
				
				atribute += attName+" ";
				if ( "discrete".equalsIgnoreCase(attType) ) {
					atribute += "{";
					for ( int i=2; i<tokens.length; i++ ) {
						atribute += tokens[i];
						if ( i+1 != tokens.length ) {
							atribute += ",";
						}
					}
					atribute += "}\n";
				} else if ( "continuous".equalsIgnoreCase(attType) ) {
					atribute += "REAL\n";
				}
				
				addLine(atribute);
				line = reader.readLine();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		addLine("\n\n@data\n\n");		
	}
	
	private void addLine(String line) throws IOException {
		writer.write(line);
		line = "";
	}
	
	public void addDataLine(String line, FeatureConstructionGenotype indiv) throws IOException {
		String lineArray[] = line.split(",");
		int cont = 0;
		FeatureConstructionGene genes[] = indiv.getGene();
		ArrayList utilizados = new ArrayList();
		
		for ( int i=0; i<genes.length; i++ ) {
			FeatureConstructionGene gen = (FeatureConstructionGene)genes[i];
			if ( utilizados.contains(gen) )	{
				continue;
			}
			else {
				utilizados.add(gen);
			}
			if ( lineArray[gen.getAttPosition()].equalsIgnoreCase(gen.getValue()) ) {
				cont ++;
			}
		}
		line = line.replaceAll("\n","");
		line = cont+","+line+"\n";
		writer.write(line);
		line = "";
	}
	
	
	public File getFile() {
		return file;
	}
	
	public void saveToDisk() throws IOException {
		writer.close();
	}
}
